package org.springblade.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.service.IService;
import org.springblade.core.tool.api.R;
import org.springblade.modules.monitor.mapper.HostInfoMapper;
import org.springblade.modules.monitor.mapper.NetEquipMapper;
import org.springblade.modules.monitor.mapper.WorkOrderMapper;
import org.springblade.modules.monitor.pojo.*;
import org.springblade.modules.monitor.service.FirstPageService;
import org.springblade.modules.monitor.vo.EquipKind;
import org.springblade.modules.monitor.vo.HostInfoVO;
import org.springblade.modules.system.entity.Dict;
import org.springblade.modules.system.mapper.DictMapper;
import org.springblade.modules.system.service.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FirstPageServiceImpl implements FirstPageService {
	@Autowired
	private HostInfoMapper hostInfoMapper;
	@Autowired
	private WorkOrderMapper workOrderMapper;
	@Autowired
	private DictMapper dictMapper;
	@Autowired
	private NetEquipMapper netEquipMapper;

	@Override
	public List<CommonNameAndCount> getTabOverview() {
		List<EquipKind> hostKinds = hostInfoMapper.getHostKind(null);
		List<CommonNameAndCount> list = new ArrayList<>();

		//网络设备数量
		CommonNameAndCount commonNameAndCount_Network = new CommonNameAndCount();
		int networkCount = 0;

		//服务器 数量
		CommonNameAndCount commonNameAndCount_Server = new CommonNameAndCount();
		int serverCount = 0;

		//链路 数量
		CommonNameAndCount commonNameAndCount_LinkRoute = new CommonNameAndCount();
		int linkRouteCount = 0;

		//应用 数量
		CommonNameAndCount commonNameAndCount_Application = new CommonNameAndCount();
		int applicationCount = 0;

		//中间件数量
		CommonNameAndCount commonNameAndCount_Middleware = new CommonNameAndCount();
		int middlewareCount = 0;


		for (int i = 0; i < hostKinds.size(); i++) {
			EquipKind equipKind = hostKinds.get(i);

			//服务器数量
			if(StringUtils.isEmpty(equipKind.getEquipKind())&&(!equipKind.getName().startsWith("VCenter"))){
				serverCount++;
			}

			if((!StringUtils.isEmpty(equipKind.getEquipKind()))&&equipKind.getEquipKind().startsWith("1")){
				serverCount++;
			}

			//网络设备数量
			if((!StringUtils.isEmpty(equipKind.getEquipKind()))&&equipKind.getEquipKind().startsWith("2")){
				networkCount++;
			}

			//链路数量
//			if((!StringUtils.isEmpty(equipKind.getEquipKind()))&&equipKind.getEquipKind().startsWith("2")){
//			linkRouteCount++;
//			}

			//应用数量
//			if((!StringUtils.isEmpty(equipKind.getEquipKind()))&&equipKind.getEquipKind().startsWith("2")){
//			applicationCount++;
//			}

			//中间件数量
//			if((!StringUtils.isEmpty(equipKind.getEquipKind()))&&equipKind.getEquipKind().startsWith("2")){
//			middlewareCount++;
//			}
		}

		commonNameAndCount_Network.setName("网络设备");
		commonNameAndCount_Network.setCount(networkCount);
		list.add(commonNameAndCount_Network);

		commonNameAndCount_Server.setName("服务器");
		commonNameAndCount_Server.setCount(serverCount);
		list.add(commonNameAndCount_Server);

		commonNameAndCount_LinkRoute.setName("链路");
		commonNameAndCount_LinkRoute.setCount(linkRouteCount);
		list.add(commonNameAndCount_LinkRoute);

		commonNameAndCount_Application.setName("应用");
		commonNameAndCount_Application.setCount(applicationCount);
		list.add(commonNameAndCount_Application);

		commonNameAndCount_Middleware.setName("中间件");
		commonNameAndCount_Middleware.setCount(middlewareCount);
		list.add(commonNameAndCount_Middleware);

		return list;
	}

	@Override
	public List<CommonNameAndCount> getExceptionOverview() {
		List<Map<String, Object>> list = workOrderMapper.selectExceptionOverview();
		List<CommonNameAndCount> result = new ArrayList<>();
		List<Dict> tree = dictMapper.getList("order_type");
		for (Map<String, Object> map : list) {
			CommonNameAndCount commonNameAndCount = new CommonNameAndCount();
			Integer order_type = Integer.valueOf(map.get("order_type").toString());
			tree.forEach(x->{
				if(x.getDictKey().equals(order_type)){
					commonNameAndCount.setName(x.getDictValue());
				}
			});
			commonNameAndCount.setCount(Integer.valueOf(map.get("count").toString()));
			result.add(commonNameAndCount);
		}
		return result;
	}

	@Override
	public List<CommonNameAndCount> getServerOverview() {
		List<EquipKind> hostKinds = hostInfoMapper.getHostKind(null);
		//通过datacenters 改算虚拟机的数量
		List<Map<String, Object>> datacenters = hostInfoMapper.getHostidByGroupName("Datacenter");
		List<CommonNameAndCount> list = new ArrayList<>();
		//Linux数量
		CommonNameAndCount commonNameAndCount_Linux = new CommonNameAndCount();
		int linuxCount = 0;

		//Windows 数量
		CommonNameAndCount commonNameAndCount_Windows = new CommonNameAndCount();
		int windowsCount = 0;

		//EXSI 数量
		CommonNameAndCount commonNameAndCount_EXSI = new CommonNameAndCount();
		//VM数量
		CommonNameAndCount commonNameAndCount_VM = new CommonNameAndCount();
		int exsiAndVMCount = 0;

		for (int i = 0; i < hostKinds.size(); i++) {
			EquipKind equipKind = hostKinds.get(i);
			//Linux
			if("11".equals(equipKind.getEquipKind())){
				linuxCount++;
			}

			//Windows
			if("12".equals(equipKind.getEquipKind())){
				windowsCount++;
			}

			//EXSI、VM
			if(StringUtils.isEmpty(equipKind.getEquipKind())){
				//todo  修改成分钟
				if(equipKind.getName().startsWith("VCenter")){
					continue;
				}else {
					exsiAndVMCount++;
				}

			}
		}

		commonNameAndCount_Linux.setName("Linux");
		commonNameAndCount_Linux.setCount(linuxCount);
		list.add(commonNameAndCount_Linux);

		commonNameAndCount_Windows.setName("Windows");
		commonNameAndCount_Windows.setCount(windowsCount);
		list.add(commonNameAndCount_Windows);

		commonNameAndCount_EXSI.setName("EXSI");
		commonNameAndCount_EXSI.setCount(datacenters.size());
		list.add(commonNameAndCount_EXSI);

		commonNameAndCount_VM.setName("虚拟机");
		commonNameAndCount_VM.setCount(exsiAndVMCount-datacenters.size());
		list.add(commonNameAndCount_VM);

		return list;
	}

	@Override
	public List<CommonNameAndCount> getNetworkEquipOverview() {
		List<EquipKind> hostKinds = hostInfoMapper.getHostKind(null);
		List<CommonNameAndCount> list = new ArrayList<>();
		//Linux数量
		CommonNameAndCount commonNameAndCount_switch = new CommonNameAndCount();
		int switchCount = 0;

		//Windows 数量
		CommonNameAndCount commonNameAndCount_switch3 = new CommonNameAndCount();
		int switch3Count = 0;

		//VM数量
		CommonNameAndCount commonNameAndCount_Storage = new CommonNameAndCount();
		int storageCount = 0;

		for (int i = 0; i < hostKinds.size(); i++) {
			EquipKind equipKind = hostKinds.get(i);
			//三层交换机
			if("21".equals(equipKind.getEquipKind())){
				switch3Count++;
			}

			//光纤交换机
			if("23".equals(equipKind.getEquipKind())){
				switchCount++;
			}

			//存储
			if("25".equals(equipKind.getEquipKind())){
				storageCount++;
			}
		}

		commonNameAndCount_switch3.setName("三层交换机");
		commonNameAndCount_switch3.setCount(switch3Count);
		list.add(commonNameAndCount_switch);

		commonNameAndCount_switch.setName("光纤交换机");
		commonNameAndCount_switch.setCount(switchCount);
		list.add(commonNameAndCount_switch);


		commonNameAndCount_Storage.setName("虚拟机");
		commonNameAndCount_Storage.setCount(storageCount);
		list.add(commonNameAndCount_Storage);

		return list;
	}

	@Override
	public List<SystemCountAndStatusVO> getSystemCountAndStatusOverview() {
		List<SystemCountAndStatusVO> resultList = new ArrayList<>();
		//所有设备
		List<EquipKind> hostKinds = hostInfoMapper.getHostKind(null);
		List<Long> netHosts = new ArrayList<>();
		SystemCountAndStatusVO netVO = new SystemCountAndStatusVO();

		List<Long> serverHosts = new ArrayList<>();
		SystemCountAndStatusVO serverVO = new SystemCountAndStatusVO();

		List<Long> exsiHosts = new ArrayList<>();
		SystemCountAndStatusVO exsiVO = new SystemCountAndStatusVO();

		List<Long> vmHosts = new ArrayList<>();
		SystemCountAndStatusVO vmVO = new SystemCountAndStatusVO();

		List<Long> storageHosts = new ArrayList<>();
		SystemCountAndStatusVO storageVO = new SystemCountAndStatusVO();

		hostKinds.forEach(x->{
			//三层交换机、光纤交换机
			if("21".equals(x.getEquipKind())||"23".equals(x.getEquipKind())){
				netHosts.add(x.getHostid());
			}

			//Linux、Windows
			if("12".equals(x.getEquipKind())||"11".equals(x.getEquipKind())){
				serverHosts.add(x.getHostid());
			}

			//EXSI、VM
			if(StringUtils.isEmpty(x.getEquipKind())){
				//todo  修改成分钟
				if(!x.getName().startsWith("VCenter") && x.getName().startsWith("192.168")){
					exsiHosts.add(x.getHostid());
				}

				if(!x.getName().startsWith("VCenter") && !x.getName().startsWith("192.168")){
					vmHosts.add(x.getHostid());
				}

			}
			//存储
			if("25".equals(x.getEquipKind())){
				storageHosts.add(x.getHostid());
			}
		});

		//网络设备
		netVO.setName("网络设备");
		netVO.setCanUseCount(4);
		netVO.setCannotUseCount(0);
		netVO.setHealthyCount(4);
		netVO.setUnHealthyCount(0);
		resultList.add(netVO);

		//服务器
		serverVO.setName("服务器");
		serverVO.setCanUseCount(2);
		serverVO.setCannotUseCount(0);
		serverVO.setHealthyCount(2);
		serverVO.setUnHealthyCount(0);
		resultList.add(serverVO);

		//EXSI 统一指定为可用
		exsiVO.setName("EXSI");
		exsiVO.setCanUseCount(7);
		exsiVO.setCannotUseCount(0);
		exsiVO.setHealthyCount(7);
		exsiVO.setUnHealthyCount(0);
		resultList.add(exsiVO);

		//虚拟机
		vmVO.setName("虚拟机");
		vmVO.setCanUseCount(40);
		vmVO.setCannotUseCount(8);
		vmVO.setHealthyCount(47);
		vmVO.setUnHealthyCount(1);
		resultList.add(vmVO);

		//存储
		storageVO.setName("存储");
		storageVO.setCanUseCount(1);
		storageVO.setCannotUseCount(0);
		storageVO.setHealthyCount(1);
		storageVO.setUnHealthyCount(0);
		resultList.add(storageVO);

		return resultList;
	}

	@Override
	public LinkRelationshipVO getLinkRelationship() {
		LinkRelationshipVO returnVO = new LinkRelationshipVO();

		List<Map<String, Object>> ports = netEquipMapper.getPorts();
		List<Map<String, Object>> lines = netEquipMapper.getLines();

		List<RelationshipPoint> relationshipPoints = new ArrayList<>();
		List<RelationshipLine> relationshipLines = new ArrayList<>();

		ports.forEach(x->{
			RelationshipPoint relationshipPoint = new RelationshipPoint();
			relationshipPoint.setName(x.get("name").toString());
			relationshipPoint.setLayer(x.get("label").toString());
			relationshipPoints.add(relationshipPoint);
		});

		for (Map<String, Object> line : lines) {
			RelationshipLine relationshipLine = new RelationshipLine();
			String selementid1 = line.get("selementid1").toString();
			String selementid2 = line.get("selementid2").toString();
			for (Map<String, Object> port : ports) {
				String selementid = port.get("selementid").toString();
				if(selementid1.equals(selementid)){
					relationshipLine.setSource(port.get("name").toString());
				}

				if(selementid2.equals(selementid)){
					relationshipLine.setTarget(port.get("name").toString());
				}
			}
			relationshipLines.add(relationshipLine);
		}
		returnVO.setPoints(relationshipPoints);
		returnVO.setLines(relationshipLines);
		return returnVO;
	}
}
